﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace GabageFactory.Models
{
    public class StackRainfallReposity : Reposity
    {
        public IEnumerable<Stack_Rainfall_Evaporation_Base_info> ListRainfalls(int stackId)
        {
            var rainfalls = from r in _dataContext.Stack_Rainfall_Evaporation_Base_infos
                        where r.stack_id == stackId
                        orderby r.month_no
                        select r;
            foreach (Stack_Rainfall_Evaporation_Base_info r in rainfalls)
            {
                yield return r;
            }
        }

        public Stack_Rainfall_Evaporation_Base_info CreateRainfall(Stack_Rainfall_Evaporation_Base_info rainfall)
        {
            _dataContext.Stack_Rainfall_Evaporation_Base_infos.InsertOnSubmit(rainfall);
            SubmitChanges();
            return rainfall;
        }

        public Stack_Rainfall_Evaporation_Base_info EditRainfall(Stack_Rainfall_Evaporation_Base_info rainfall)
        {
            SubmitChanges();
            return rainfall;
        }

        public Stack_Rainfall_Evaporation_Base_info GetRainfallByStackIdAndMonthNo(long stackId, int monthNo)
        {
            var rainfall = from r in _dataContext.Stack_Rainfall_Evaporation_Base_infos
                       where stackId == r.stack_id && r.month_no == monthNo 
                       select r;

            if (rainfall.Count() > 0)
            {
                return rainfall.First();
            }
            else return null;
        }
    }
}